import '@tarojs/async-await'
import Taro, { Component } from '@tarojs/taro'
import { Provider } from '@tarojs/redux'
import 'taro-ui/dist/style/index.scss'
import { configStore } from './store'
import './app.less'
import { getStorageSync, removeStorageSync } from './crossplatform/apiservice/storage'
import { UPDATEUSERINFO, CLEARUSERINFO } from './store/types/userinfo'
import { CLEARLANDMARK, UPDATELANDMARK } from './store/types/landmark'
import { setGlobalData } from './configuration/globaldata'
import { getBlockWords } from './service/user-controller'

// 如果需要在 h5 环境中开启 React Devtools
// 取消以下注释：
// if (process.env.NODE_ENV !== 'production' && process.env.TARO_ENV === 'h5')  {
//   require('nerv-devtools')
// }

const store = configStore()

class App extends Component {
  config = {
    networkTimeout: {
      request: 10000,
      downloadFile: 10000
    },
    pages: [
      'pages/start-page/index', // 启动页
      'pages/job-view/index', // 首页
      'pages/self-center/index', // 我的
      // 'pages/job-list/index',
      'pages/company-homepage/index', // B-企业主页
      'pages/job-detail/index', // 岗位详情
      'pages/self-message/index', // 填写资料
      'pages/phone-validation/index', // 验证码
      'pages/my-resume/index', // 我的简历
      'pages/education-information/index', // 简历-教育信息
      'pages/work-exp/index', // 简历-工作经历
      'pages/choose-job-types/index', // 简历-求职意向-岗位分类
      'pages/choose-job-input/index', // input输入页面
      'pages/job-collection/index', // 职位收藏
      'pages/person-detail/index', // 个人详情
      'pages/avator-page/index', // 上传头像
      'pages/setting-page/index', // 设置
      'pages/new-job-intention/index', // 求职意向
      'pages/edit-job-intention/index', // 编辑和添加求职意向
      'pages/choose-work-nature/index', // 求职意向-选择工作性质
      'pages/choose-job-area/index', // 求职意向-选择工作区域
      'pages/choose-desire-job/index', // 求职意向-选择期望职位
      'pages/new-work-exp/index', // 工作经验
      'pages/new-educationa-information/index', // 求职意向-教育信息
      'pages/my-album/index', // 我的相册
      'pages/message-list/index' // 消息列表
    ],
    window: {
      navigationBarBackgroundColor: '#fff',
      navigationBarTitleText: '人职初',
      navigationBarTextStyle: 'black',
      backgroundTextStyle: 'dark'
    },
    tabBar: {
      color: '#000',
      selectedColor: '#000',
      borderStyle: 'black',
      backgroundColor: '#f8f8f8',
      list: [
        {
          pagePath: 'pages/job-view/index',
          text: '首页',
          iconPath: 'static/images/icon_1.png',
          selectedIconPath: 'static/images/icon_1_selected.png'
        },
        {
          pagePath: 'pages/message-list/index',
          text: '消息',
          iconPath: 'static/images/icon_2.png',
          selectedIconPath: 'static/images/icon_2_selected.png'
        },
        {
          pagePath: 'pages/self-center/index',
          text: '我的',
          iconPath: 'static/images/icon_3.png',
          selectedIconPath: 'static/images/icon_3_selected.png'
        }
      ]
    },
    subpackages: [
      {
        root: 'subpackages',
        pages: [
          'pages/company-represent-infor/index', // 填写公司个人资料
          'pages/post-job-message/index', // B-职位发布提醒页
          'pages/post-part-time/index', // B-发布职位
          'pages/add-work-address/index', // B-选择工作地点
          'pages/add-company-address/index', // B-添加公司地址
          'pages/create-company/index', // 输入选择学校/公司
          'pages/create-new-cp/index', // B/C-创建新的公司
          'pages/join-new-cp/index', // B/C-申请加入新的公司
          'pages/my-company/index', // B-我的企业
          'pages/my-company-list/index', // B-公司列表/切换公司
          'pages/my-company-apply/index', // B-我的申请公司列表
          'pages/my-colleague/index', // B-我的同事
          'pages/people-collection/index', // B-人才收藏
          'pages/manage-jobs/index', // B-职位管理
          'pages/company-update/index', // B-企业主页信息填写
          'pages/add-things-company/index', // B-添加企业标签
          'pages/agreement-pages/index', // 用户使用协议页面
          'pages/job-address-manage/index', // B-添加工作地点
          'pages/feed-back/index', // 意见反馈
          // 'pages/apply-supercompany/index',
          'pages/vip-center/index', // B-会员主页
          'pages/vip-recharge/index', // B-会员充值页
          'pages/vip-right/index', // B-岗位发布-会员特权
          'pages/order-list/index', // B-订单记录
          'pages/talent-communicate/index', // 人才沟通
          'pages/vip-rule/index' // vip协议
        ]
      },
      {
        root: 'packageC',
        pages: [
          'pages/onekey-login/index', // 一键登录页面
          'pages/agreement-page/index', // 用户协议
          'pages/base-information/index', // 基本信息页面
          'pages/company-report/index', // 举报页面
          'pages/chat-page/index', // 对话页面
          'pages/city-location/index', // 切换城市
          'pages/search-page/index', // 搜索
          'pages/post-rule-page/index' // 发布规则页面
        ]
      }
    ],
    preloadRule: {
      'pages/setting-page/index': {
        network: 'all',
        packages: ['subpackages']
      },
      'pages/job-view/index': {
        network: 'all',
        packages: ['packageC']
      }
    },
    permission: {
      'scope.userLocation': {
        desc: '您的位置信息将用于查询距离最近的工作地点'
      }
    }
  }

  componentWillMount() {
    // onLaunch
    // removeStorageSync('userinfo') // 小程序启动删除缓存数据
    // store.dispatch({
    //   type: CLEARUSERINFO,
    //   payload: {}
    // })
    this.doOnLaunch()
  }

  doOnLaunch = () => {
    // this.userInfoSyncToReducer()
    this.landmarkSyncToReducer()
    // this.getBlockWords()
  }

  userInfoSyncToReducer = () => {
    const userinfo = getStorageSync('userinfo') || {}
    if (userinfo && userinfo.userId) {
      store.dispatch({
        type: UPDATEUSERINFO,
        payload: {
          ...userinfo
        }
      })
    }
  }

  landmarkSyncToReducer = () => {
    const landmark = getStorageSync('landmark') || {}
    let oDateLong = new Date().getTime()
    if (landmark && landmark.CacheTime) {
      if (oDateLong >= landmark.CacheTime) {
        store.dispatch({
          type: CLEARLANDMARK,
          payload: {}
        })
      } else {
        store.dispatch({
          type: UPDATELANDMARK,
          payload: {
            ...landmark
          }
        })
      }
    }
    store.dispatch({
      type: UPDATELANDMARK,
      payload: {
        ...landmark
      }
    })
  }

  getBlockWords = () => {
    // 获取屏蔽字
    // setGlobalData
    getBlockWords().then(({ data: {data, code} }) => {
      if(code === 200) {
        let blockWords = ''
        let blockWordsArr = data.split('/')
        blockWordsArr.map(item => {
          item.replace('\\', '\\\\')
          blockWords += item + '|'
          return item
        })
        setGlobalData('blockWords', new RegExp(`${blockWords}\\d{6,}`, 'i'))
      } else {

      }
    })
  }

  render() {
    return (
      <Provider store={store}>
        <HomePage />
      </Provider>
    )
  }
}

Taro.render(<App />, document.getElementById('app'))
